﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Repository;
using SurveyAutomation.Properties;

namespace SurveyAutomation
{
    public partial class LanguageManager : Form
    {
        private readonly SqLiteDatabase _repo;

        public LanguageManager()
        {
            InitializeComponent();

            _repo = new SqLiteDatabase();
        }

        private void LanguageManager_Load(object sender, EventArgs e)
        {
            BindLangData();
            txtName.Focus();
        }

        private void gvLang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvLang.SelectedCells != null && gvLang.SelectedCells.Count > 0)
            {
                var i = gvLang.SelectedCells[0].RowIndex;
                txtId.Text = gvLang.Rows[i].Cells[0].Value.ToString();
                txtName.Text = gvLang.Rows[i].Cells[1].Value.ToString();
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (btnAddNew.Text == Resources.Add)
            {
                gvLang.Enabled = false;
                btnAddNew.Text = Resources.Save;
                btnUpdate.Text = Resources.Cancel;
            }
            else
            {
                // Add new Language
                var returnValue = _repo.InsertLanguage(txtName.Text.Trim());

                if (returnValue > 0)
                {
                    // 
                    BindLangData();

                    UtilsHelper.ShowMessage(Resources.Save_successfully, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    UtilsHelper.ShowMessage(Resources.Can_not_save__please_try_again_later_or_contact_administrator, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                gvLang.Enabled = true;
                btnAddNew.Text = Resources.Add;
                btnUpdate.Text = Resources.Update;
            }

            ClearFormValue();
        }

        private void BindLangData()
        {
            var languages = _repo.GetLanguages();
            if (languages != null)
            {
                gvLang.DataSource = languages;
            }
        }


        private void ClearFormValue()
        {
            txtId.Text = "";
            txtName.Text = "";
            txtName.Focus();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text == Resources.Cancel)
            {
                ClearFormValue();
                btnAddNew.Text = Resources.Add;
                btnUpdate.Text = Resources.Update;
                gvLang.Enabled = true;
            }
            else
            {
                // update Lang
                var returnVal = _repo.UpdateLanguage(int.Parse(txtId.Text), txtName.Text.Trim());
                if (returnVal != -1)
                {
                    BindLangData();
                    UtilsHelper.ShowMessage(Resources.Update_successfully, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    UtilsHelper.ShowMessage(Resources.Can_not_save__please_try_again_later_or_contact_administrator, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ClearFormValue();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var dlgResult = UtilsHelper.ShowMessage("Are you sure you want to delete this language",
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dlgResult == DialogResult.Yes)
            {
                var i = gvLang.SelectedCells[0].RowIndex;
                if (i != -1)
                {
                    var langId = gvLang.Rows[i].Cells[0].Value.ToString();
                    // delete lang
                    var returnVal = _repo.DeleteLanguage(int.Parse(langId));
                    if (returnVal == -1)
                    {
                        UtilsHelper.ShowMessage(Resources.Can_not_delete__please_try_again_or_contact_administrator, MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        UtilsHelper.ShowMessage(Resources.Delete_successfully, MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                        ClearFormValue();
                        BindLangData();
                    }
                }
                else
                {
                    UtilsHelper.ShowMessage(Resources.Please_select_language_in_the_grid_to_delete, MessageBoxButtons.OK,
                                            MessageBoxIcon.Information);
                }

            }
        }
    }
}
